﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL ;
using dhSystemBLL.orderData ;
using baseSet.importExport;
using baseSet;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using dhsystem.basicForm;


namespace dhsystem.orderForm
{
    public partial class FrmProductAdd : Form
    {
        orderDataSet.productUnitDataTable prodcutunitTable = new orderDataSet.productUnitDataTable();
        productTypeBLL productType = new productTypeBLL();
        productListModel productRow = new productListModel();
        productListBLL product = new productListBLL();
        productUnitBLL prodcutunit = new productUnitBLL();
        productUnitModel productunitRow = new productUnitModel();
        BindingSource bindingSourceProductAdd;
        string state ; 
        //绑定数据源
        public FrmProductAdd(BindingSource bindingSourceProductList, string _state)
        {
            bindingSourceProductAdd = bindingSourceProductList;
            state = _state;
            InitializeComponent();
        }
      
        //保存按钮，插入或修改数据
      public  void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                if (textPname.Text  == "")
                {
                    MessageBox.Show("请输入产品名称");
                }
                else
                {
                    try
                    {
                        DialogResult result = MessageBox.Show("是否新增产品！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            productRow.pname = textPname.Text;
                            productRow.pnameMark = textPnameMark.Text;
                            productRow.ptype = comboBoxptype.Text;
                            productRow.pmode = textpmode.Text;
                            productRow.pattr = textpattr.Text;
                            productRow.createUser = GlobalInfo.UserName;
                            productRow.updateUser = GlobalInfo.UserName;
                            productRow.createDate = DateTime.Now;
                            productRow.updateDate = DateTime.Now;
                            productRow.mark = textmark.Text;
                            productRow.stockposition = textstockposition .Text ;
                            productRow.qcStatus = true;
                            productRow.stockQty = 0;
                            product.productListInsert(productRow);
                            bindingSourceproductList.DataSource = product.getproductListAll();
                            MessageBox.Show("新增产品成功");
                        }
                    }
                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改产品！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        productRow.grwight =Convert.ToDecimal ( textgrwight.Text);
                        productRow.recordid = Convert.ToInt32(textrecordid.Text);
                        productRow.pname = textPname.Text;
                        productRow.pnameMark = textPnameMark.Text;
                        productRow.ptype = comboBoxptype.Text;
                        productRow.pmode = textpmode.Text;
                        productRow.pattr = textpattr.Text;
                        productRow.updateUser = GlobalInfo.UserName;
                        productRow.createUser = GlobalInfo.UserName;
                        productRow.createDate = DateTime.Now;
                        productRow.updateDate = DateTime.Now;
                        productRow.mark = textmark.Text;
                        productRow.stockposition = textstockposition.Text;
                        productRow.qcStatus = true;
                        product.productListUpdate(productRow);
                        bindingSourceproductList.DataSource = product.getproductListAll();
                        MessageBox.Show("修改产品成功");

                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
           
        }
        //绑定数据源
        private void FrmProductAdd_Load(object sender, EventArgs e)
        {
           
            bindingSourceproductType.DataSource = productType.getproductTypeAll();
            comboBoxptype.DataSource = bindingSourceproductType;
            comboBoxptype.DisplayMember = "ptype";
            comboBoxptype.ValueMember = "ptype";
     
            if (state == "编辑")
            {
                textgrwight.DataBindings.Add("text", bindingSourceProductAdd, "grwight");
                textrecordid.DataBindings.Add("text", bindingSourceProductAdd, "recordid");
                textPname.DataBindings.Add("Text", bindingSourceProductAdd, "pname");
                textPnameMark.DataBindings.Add("text", bindingSourceProductAdd, "pnameMark");
                comboBoxptype.DataBindings.Add("text", bindingSourceProductAdd, "ptype");
                textpmode.DataBindings.Add("text", bindingSourceProductAdd, "pmode");
                textpattr.DataBindings.Add("text", bindingSourceProductAdd, "pattr");
                textmark.DataBindings.Add("text", bindingSourceProductAdd, "mark");
                textstockposition.DataBindings.Add("text", bindingSourceProductAdd, "stockposition");
                }
                textPname.Focus();
                

            
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
    }
}
